<?php
	require_once("user_session.php");
	require_once("scripts/EKTalk.php");
	
	$template = new Template();
	$template->set_file("talk","template/talk_create.html");
	
	# Set page title
	$template->set_var("pageTitle","[+ Talk] :: ".SITENAME);

	if(!isset($_SESSION['userid'])||$_SESSION['userid']==""){
			$template->set_var("note","Login to talk!");
			$template->set_var("contentView", "hidden");
	} else{
		if(isset($_POST["content"]))
		{
			# means it is the action page
			$thisTalkId = EKTalk::writeNewTalk($_POST["ownerId"], $_POST["content"], $_POST["talkType"]);
			EKUser::insertRecord($_SESSION['userid'],"t",$thisTalkId);
			
			# Save talk create event
			$event = new EKEvent($_SESSION['userid'],"create","t",$thisTalkId);
			$event->save();
			
			switch($_POST["talkType"])
			{	
				case "d":
					$docId = $_POST["ownerId"];
					break;
				case "p":
					$docId = EKParagraph::getOwnerIdById($_POST["ownerId"]);
					break;
			}
			$relocation = "<script language = \"javascript\">\n".
						"alert(\"Talk published!\"); \n".
						"self.location=\"document.php?id=".$docId."&method=v\"; \n".
						"</script>";
			$template->set_var("note",$relocation);
		}else{
			// means it is the filling page
			setTemplateWithSession($template, "talk_create.php?ownerId=".$_GET["ownerId"]."&talkType=".$_GET["talkType"]);
			
			switch($_GET["talkType"])
			{
				case "d":
					$myDoc = new EKDocument($_GET["ownerId"]);
					$ownerShow = "You are giving talk to the document <b>".$myDoc->getTitle()."</b>";		
					break;
				case "p":
					$para = new EKParagraph($_GET["ownerId"]);
					$ownerShow = $para->getContent();
					break;
			}
			
			$template->set_var("note", $ownerShow);
			$template->set_var("talk_author", $_SESSION["username"]);
			$template->set_var("ownerId", $_GET["ownerId"]);
			$template->set_var("talkType", $_GET["talkType"]);
		}
	}
		
	# Parse & generate the output
	$template->parse("output","talk");
	$template->p("output");
?>